﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Windows.Forms;

namespace BaseHelper.Base
{

    public class AutoRunHelper
    {

        private static string strAppName="rtsystem";

        public static bool SetAutoRun(bool bIsAutoRun)
        {
            try
            {

                string strAppPath = "\"" + Application.ExecutablePath +"\" -autorun";

                RegistryKey reg = Registry.LocalMachine;
                RegistryKey run = reg.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
                if (bIsAutoRun)
                {
                    run.SetValue(strAppName, strAppPath);
                }
                else
                {
                    if (null != run.GetValue(strAppName))
                    {
                        run.DeleteValue(strAppName);
                    }
                }
                run.Close();
                reg.Close();
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Err("RT.Helper.Base.AutoRunHelper.cs", "SetAutoRun()", ex);

                return false;
            }
        }

        public static bool IsAutoRun()
        {
            try
             {
                 string strAppPath = "\"" + Application.ExecutablePath + "\" -autorun";
                RegistryKey reg = Registry.LocalMachine;
                RegistryKey software = reg.OpenSubKey(@"SOFTWARE");
                RegistryKey run = reg.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
                object key = run.GetValue(strAppName);
                software.Close();
                run.Close();
                if (null == key || !strAppPath.Equals(key.ToString()))
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Err("RT.Helper.Base.AutoRunHelper.cs", "IsAutoRun()", ex);
            }
            return false;
        }
    }
}